import requests
import downFile
import json
def process_detail(data):
    """
    处理标题列表，获取详情页pdf附件的URL
    :param titles: 标题列表
    :return: 包含“物资”的附件列表
    """
    detailUrl='https://ecp.sgcc.com.cn/ecp2.0/ecpwcmcore//index/getWinFile'

    # 设置请求头，模拟浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'Content-Length': str(len(json.dumps(data).encode('utf-8'))),
        'Content-Type': 'application/json',
        'host': 'ecp.sgcc.com.cn'
    }

    # 模拟浏览器请求头
    download_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'host': 'ecp.sgcc.com.cn'
    }
    matching_datas = []
    for item in data:
        # JSON参数
        body = item['id']
        # 发送POST请求
        response = requests.post(detailUrl, json=body, headers=headers)
        if response.status_code == 200:
            # 将响应内容转换成JSON对象
            response_data = response.json()

            # 提取所需数据
            if 'resultValue' in response_data:
                file_list = response_data.get('resultValue').get('files')
                if file_list and len(file_list) > 0:
                    for file in file_list:
                        fileUrl='https://ecp.sgcc.com.cn/ecp2.0/ecpwcmcore//index/showPDF?filePath='+file['FILE_PATH']
                        save_path="F:/ecp/"+file['FILE_NAME']
                        downFile.download_pdf(fileUrl,save_path,download_headers)
                else:
                    print(f"\033[91m[ERROR] "+item['title']+"不存在附件，无需下载\033[0m")